(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.ppfProgram.updateController', updateController);
	updateController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'ppfProgramService', '$filter' ];
	function updateController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, ppfProgramService, $filter) {
		var ctrl = this, ppfProgramInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.initValidatorConfig = angular.copy(ctrl.validatorConfig);
		ctrl.validatorConfig = {};
		ctrl.ppfProgram = {};
		
		ctrl.init = function() {
			var params = {
					programId : ppfProgramInfo.programId
			};
			// init acct
			ppfProgramService.initProfAcct(layoutConfig, $scope, ctrl, true);
			// init int
			//ppfProgramService.initProfInt(layoutConfig, $scope, ctrl, true);
			//ppfProgramService.initProfCostOfFundInt(layoutConfig, $scope, ctrl, true);
			// init pastdue
			ppfProgramService.initProfPastdue(layoutConfig, $scope, ctrl, true);
			// init fee
			ppfProgramService.initProfFee(layoutConfig, $scope, ctrl, true);
			// init doc
			ppfProgramService.initEventDoccfg(layoutConfig, $scope, ctrl, true);
			// init limit facility
			//ppfProgramService.initProfLimitFaci(layoutConfig, $scope, ctrl, true);
			//ppfProgramService.initProfLimitInsFaci(layoutConfig, $scope, ctrl, true);
			//ppfProgramService.initProfLimitPurFaci(layoutConfig, $scope, ctrl, true);
			// init Settlement Instruction
			ppfProgramService.initProfSetlinst(layoutConfig, $scope, ctrl, true);
			// init cust prop define
			ppfProgramService.initCustProp(layoutConfig, $scope, ctrl, true);
			//
			ppfProgramService.initGeneral($scope, ctrl, true).then(function() {
				authService.update(params).then(function(response) {
					var initPpfProgram = response.data || ppfProgramInfo;
					ctrl.action = "update";
					ppfProgramService.initLayoutConfigAndModel($scope, ctrl, initPpfProgram, ctrl.action, true).then(function() {
					});
				});
			});
		};

		ctrl.updateSave = function() {
			ppfProgramService.clearDirtyData(ctrl);
			if (ctrl.ppfProgram.taskId) {
				return authService.updateContinueSave(ctrl.ppfProgram).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.ppfProgram).then(saveSuccessful, saveFailed);
			} 
		};

		ctrl.updateSubmit = function() {
			ppfProgramService.clearDirtyData(ctrl);
			if (ctrl.ppfProgram.taskId) {
				return authService.updateContinueSubmit(ctrl.ppfProgram).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.ppfProgram).then(submitSuccessful, submitFailed);
			}
		};
		
		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.ppfProgram.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.ppfprogram.list',
					'state' : 'cda.ppfProgram.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
